/*
 * This file is not available for use in any application other than as a
 * MATLAB(R) MEX file for use with the Simulink(R) product.
 */

/*
 * simulink_blms_acc.c
 *
 * Real-Time Workshop code generation for Simulink model "simulink_blms_acc.mdl".
 *
 * Model version              : 1.1
 * Real-Time Workshop version : 7.5  (R2010a)  25-Jan-2010
 * C source code generated on : Mon Jun 13 14:41:51 2011
 *
 * Target selection: accel.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: 32-bit Generic
 * Emulation hardware selection:
 *    Differs from embedded hardware (MATLAB Host)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */
#include <math.h>
#include "simulink_blms_acc.h"
#include "simulink_blms_acc_private.h"
#include <stdio.h>
#include "simstruc.h"
#include "fixedpoint.h"
#define CodeFormat                     S-Function
#define AccDefine1                     Accelerator_S-Function

/* Outputs for root system: '<Root>' */
static void mdlOutputs(SimStruct *S, int_T tid)
{
  /* local block i/o variables */
  real_T B_0_24_0;
  real_T B_0_2_0[16];
  real_T B_0_4_0[16];
  real_T B_0_5_2[512];
  real_T currentTime;
  real_T B_0_17_0;
  if (ssIsSampleHit(S, 1, tid)) {
    /* FromWorkspace: '<Root>/From Workspace' */
    /* Call into Simulink for From Workspace */
    ssCallAccelRunBlock(S, 0, 0, SS_CALL_MDL_OUTPUTS);

    /* Scope: '<Root>/Scope2' */

    /* Call into Simulink for Scope */
    ssCallAccelRunBlock(S, 0, 1, SS_CALL_MDL_OUTPUTS);
  }

  /* Signal Processing Blockset Buffer/Unbuffer (sdsprebuff2) - '<Root>/B_0_2' */
  {
    /* Copy input samples to buffer */
    if (ssIsSampleHit(S, 1, tid)) {
      real_T **inBufPtr = (real_T **)&((D_Work *) ssGetRootDWork(S))
        ->Buffer_IN_BUF_PTR;
      real_T *memBase = (real_T *)&((D_Work *) ssGetRootDWork(S))
        ->Buffer_CircBuff[0];
      **inBufPtr = ((BlockIO *) _ssGetBlockIO(S))->B_0_0_0;
      *inBufPtr = (memBase + 31 - *inBufPtr) ? (*inBufPtr+1) : memBase;
    }

    /* Copy output samples from buffer */
    if (ssIsSampleHit(S, 2, tid)) {
      MWDSP_Buf_OutputFrame_1ch((byte_T *)B_0_2_0, (byte_T **)&((D_Work *)
        ssGetRootDWork(S))->Buffer_OUT_BUF_PTR, (byte_T *)&((D_Work *)
        ssGetRootDWork(S))->Buffer_CircBuff[0], ((D_Work *) ssGetRootDWork(S))
        ->Buffer_SHIFT_PER_ELEM, 32 * sizeof(real_T) ,16, 0);
    }
  }

  if (ssIsSampleHit(S, 1, tid)) {
    /* FromWorkspace: '<Root>/From Workspace1' */
    /* Call into Simulink for From Workspace */
    ssCallAccelRunBlock(S, 0, 3, SS_CALL_MDL_OUTPUTS);
  }

  /* Signal Processing Blockset Buffer/Unbuffer (sdsprebuff2) - '<Root>/B_0_4' */
  {
    /* Copy input samples to buffer */
    if (ssIsSampleHit(S, 1, tid)) {
      real_T **inBufPtr = (real_T **)&((D_Work *) ssGetRootDWork(S))
        ->Buffer1_IN_BUF_PTR;
      real_T *memBase = (real_T *)&((D_Work *) ssGetRootDWork(S))
        ->Buffer1_CircBuff[0];
      **inBufPtr = ((BlockIO *) _ssGetBlockIO(S))->B_0_3_0;
      *inBufPtr = (memBase + 31 - *inBufPtr) ? (*inBufPtr+1) : memBase;
    }

    /* Copy output samples from buffer */
    if (ssIsSampleHit(S, 2, tid)) {
      MWDSP_Buf_OutputFrame_1ch((byte_T *)B_0_4_0, (byte_T **)&((D_Work *)
        ssGetRootDWork(S))->Buffer1_OUT_BUF_PTR, (byte_T *)&((D_Work *)
        ssGetRootDWork(S))->Buffer1_CircBuff[0], ((D_Work *) ssGetRootDWork(S)
        )->Buffer1_SHIFT_PER_ELEM, 32 * sizeof(real_T) ,16, 0);
    }
  }

  if (ssIsSampleHit(S, 2, tid)) {
    /* Signal Processing Blockset Block LMS adaptive filter (sdspblms) - '<Root>/B_0_5' - Output */
    {
      /* check for reset */
      if (0.0 != (real_T)0) {
        /* copy Weight IC from RTP to Weight IC buffer (DWork) */
        {
          int_T i;
          for (i = 0; i < (int_T)512U; i++) {
            ((D_Work *) ssGetRootDWork(S))->BlockLMSFilter_WGT_IC_DWORK[i] =
              rtConstP.pooled1;
          }
        }
      }

      MWDSP_blms_an_wy_DD(
                          (const real_T*)B_0_2_0,
                          (const real_T*)B_0_4_0,
                          (const real_T)(*(real_T *)&((Parameters *)
        ssGetDefaultParam(S))->P_1),
                          (real_T*)&((D_Work *) ssGetRootDWork(S))
                          ->BlockLMSFilter_IN_BUFFER_DWORK[0],
                          (real_T*)&((D_Work *) ssGetRootDWork(S))
                          ->BlockLMSFilter_WGT_IC_DWORK[0],
                          (int_T)512U,
                          (int_T)16U,
                          (const real_T)(*(real_T *)&((Parameters *)
        ssGetDefaultParam(S))->P_0),
                          (int_T)16,
                          (real_T*)((BlockIO *) _ssGetBlockIO(S))->B_0_5_0,
                          (real_T*)((BlockIO *) _ssGetBlockIO(S))->B_0_5_1
                          ,(real_T*)B_0_5_2
                          );
    }
  }

  /* Signal Processing Blockset Buffer/Unbuffer (sdsprebuff2) - '<Root>/B_0_6' */
  {
    /* Copy input samples to buffer */
    if (ssIsSampleHit(S, 2, tid)) {
      MWDSP_Buf_CopyFrame_OL_1ch((const byte_T *)((BlockIO *) _ssGetBlockIO(S)
        )->B_0_5_0, (byte_T **)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer_IN_BUF_PTR, (byte_T *)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer_CircBuff[0], ((D_Work *) ssGetRootDWork(S))
        ->Unbuffer_SHIFT_PER_ELEM, 32 * sizeof(real_T) ,16);
    }

    /* Copy output samples from buffer */
    if (ssIsSampleHit(S, 1, tid)) {
      real_T **outBufPtr = (real_T **)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer_OUT_BUF_PTR;
      real_T *memBase = (real_T *)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer_CircBuff[0];
      ((BlockIO *) _ssGetBlockIO(S))->B_0_8_0 = **outBufPtr;
      *outBufPtr = ((memBase + 31 - *outBufPtr) <= 0) ? memBase : (*outBufPtr+1);
    }
  }

  if (ssIsSampleHit(S, 1, tid)) {
    /* Scope: '<Root>/Scope4' */
    /* Call into Simulink for Scope */
    ssCallAccelRunBlock(S, 0, 7, SS_CALL_MDL_OUTPUTS);
  }

  /* Signal Processing Blockset Buffer/Unbuffer (sdsprebuff2) - '<Root>/B_0_8' */
  {
    /* Copy input samples to buffer */
    if (ssIsSampleHit(S, 2, tid)) {
      MWDSP_Buf_CopyFrame_OL_1ch((const byte_T *)((BlockIO *) _ssGetBlockIO(S)
        )->B_0_5_1, (byte_T **)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer1_IN_BUF_PTR, (byte_T *)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer1_CircBuff[0], ((D_Work *) ssGetRootDWork(S))
        ->Unbuffer1_SHIFT_PER_ELEM, 32 * sizeof(real_T) ,16);
    }

    /* Copy output samples from buffer */
    if (ssIsSampleHit(S, 1, tid)) {
      real_T **outBufPtr = (real_T **)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer1_OUT_BUF_PTR;
      real_T *memBase = (real_T *)&((D_Work *) ssGetRootDWork(S))
        ->Unbuffer1_CircBuff[0];
      ((BlockIO *) _ssGetBlockIO(S))->B_0_8_0 = **outBufPtr;
      *outBufPtr = ((memBase + 31 - *outBufPtr) <= 0) ? memBase : (*outBufPtr+1);
    }
  }

  if (ssIsSampleHit(S, 1, tid)) {
    /* Scope: '<Root>/Scope5' */
    /* Call into Simulink for Scope */
    ssCallAccelRunBlock(S, 0, 9, SS_CALL_MDL_OUTPUTS);

    /* Scope: '<Root>/Scope6' */

    /* Call into Simulink for Scope */
    ssCallAccelRunBlock(S, 0, 10, SS_CALL_MDL_OUTPUTS);

    /* ToWorkspace: '<Root>/To Workspace' */

    /* Call into Simulink for To Workspace */
    ssCallAccelRunBlock(S, 0, 11, SS_CALL_MDL_OUTPUTS);
    ((BlockIO *) _ssGetBlockIO(S))->B_0_12_0 = ((Parameters *) ssGetDefaultParam
      (S))->P_2;
  }

  if (ssIsContinuousTask(S, tid)) {
    currentTime = ssGetTaskTime(S,tid);
    if (currentTime < ((Parameters *) ssGetDefaultParam(S))->P_3) {
      currentTime = ((Parameters *) ssGetDefaultParam(S))->P_4;
    } else {
      currentTime = ((Parameters *) ssGetDefaultParam(S))->P_5;
    }

    /* Clock: '<S1>/Clock' */
    B_0_24_0 = ssGetT(S);
  }

  if (ssIsSampleHit(S, 1, tid)) {
    ((BlockIO *) _ssGetBlockIO(S))->B_0_15_0 = ((Parameters *) ssGetDefaultParam
      (S))->P_6;
  }

  if (ssIsContinuousTask(S, tid)) {
    B_0_17_0 = (B_0_24_0 - ((BlockIO *) _ssGetBlockIO(S))->B_0_15_0) *
      currentTime;
  }

  if (ssIsSampleHit(S, 1, tid)) {
    ((BlockIO *) _ssGetBlockIO(S))->B_0_18_0 = ((Parameters *) ssGetDefaultParam
      (S))->P_7;
  }

  if (ssIsContinuousTask(S, tid)) {
    B_0_24_0 = muDoubleScalarSin(((Parameters *) ssGetDefaultParam(S))->P_10 *
      ssGetTaskTime(S,tid) + ((Parameters *) ssGetDefaultParam(S))->P_11) *
      ((Parameters *) ssGetDefaultParam(S))->P_8 + ((Parameters *)
      ssGetDefaultParam(S))->P_9;
    ((BlockIO *) _ssGetBlockIO(S))->B_0_23_0 = muDoubleScalarExp((B_0_17_0 +
      ((BlockIO *) _ssGetBlockIO(S))->B_0_18_0) * ((BlockIO *) _ssGetBlockIO(S)
      )->B_0_12_0) * B_0_24_0;
    B_0_24_0 = muDoubleScalarSin(((Parameters *) ssGetDefaultParam(S))->P_14 *
      ssGetTaskTime(S,tid) + ((Parameters *) ssGetDefaultParam(S))->P_15) *
      ((Parameters *) ssGetDefaultParam(S))->P_12 + ((Parameters *)
      ssGetDefaultParam(S))->P_13;
    ((BlockIO *) _ssGetBlockIO(S))->B_0_25_0 = ((BlockIO *) _ssGetBlockIO(S))
      ->B_0_23_0 + B_0_24_0;
  }

  if (ssIsSampleHit(S, 1, tid)) {
    /* Scope: '<Root>/Scope1' */
    /* Call into Simulink for Scope */
    ssCallAccelRunBlock(S, 0, 26, SS_CALL_MDL_OUTPUTS);

    /* Scope: '<Root>/Scope3' */

    /* Call into Simulink for Scope */
    ssCallAccelRunBlock(S, 0, 27, SS_CALL_MDL_OUTPUTS);
  }
}

/* Update for root system: '<Root>' */
#define MDL_UPDATE

static void mdlUpdate(SimStruct *S, int_T tid)
{
  /* tid is required for a uniform function interface.
   * Argument tid is not used in the function. */
  UNUSED_PARAMETER(tid);
}

/* Function to initialize sizes */
static void mdlInitializeSizes(SimStruct *S)
{
  /* checksum */
  ssSetChecksumVal(S, 0, 997865206U);
  ssSetChecksumVal(S, 1, 2936339141U);
  ssSetChecksumVal(S, 2, 959733051U);
  ssSetChecksumVal(S, 3, 3207991016U);

  /* options */
  ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);

  /* Accelerator check memory map size match for DWork */
  if (ssGetSizeofDWork(S) != sizeof(D_Work)) {
    ssSetErrorStatus(S,"Unexpected error: Internal DWork sizes do "
                     "not match for accelerator mex file.");
  }

  /* Accelerator check memory map size match for BlockIO */
  if (ssGetSizeofGlobalBlockIO(S) != sizeof(BlockIO)) {
    ssSetErrorStatus(S,"Unexpected error: Internal BlockIO sizes do "
                     "not match for accelerator mex file.");
  }

  /* model parameters */
  _ssSetDefaultParam(S, (real_T *) &rtDefaultParameters);
}

/* mdlInitializeSampleTimes function (used to set up function-call connections) */
static void mdlInitializeSampleTimes(SimStruct *S)
{
}

/* Empty mdlTerminate function (never called) */
static void mdlTerminate(SimStruct *S)
{
}

/* MATLAB MEX Glue */
#include "simulink.c"
